package com.bawei.play.adapter

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.LinearInterpolator
import androidx.recyclerview.widget.RecyclerView
import com.bawei.common.entity.SimpleVideoEntity
import com.bawei.image.ImgOptions
import com.bawei.image.LoadImageUtil
import com.bawei.play.databinding.ItemPlayVideoBinding

/**
 * Author: Jason Zhang
 * Date: 2025/2/6
 * Desc: 视频播放数据适配器
 */
class PlayVideoAdapter constructor(private val list: MutableList<SimpleVideoEntity>): RecyclerView.Adapter<PlayVideoAdapter.VideoPlayViewHolder>() {

    private var itemClickListener: OnItemChildClickListener? = null

    inner class VideoPlayViewHolder(val dataBinding: ItemPlayVideoBinding): RecyclerView.ViewHolder(dataBinding.root)

    fun setOnItemClickListener(listener: OnItemChildClickListener){
        itemClickListener = listener
    }

    override fun getItemCount(): Int = list.size

    override fun getItemViewType(position: Int): Int {
        return 0
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoPlayViewHolder {
        return VideoPlayViewHolder(ItemPlayVideoBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun onBindViewHolder(holder: VideoPlayViewHolder, position: Int) {
        var entity = list[position]
        //视频播放器
        holder.dataBinding.gsyPlayer.apply {
            setUp(entity.videopath, true, "")
            backButton.visibility = View.GONE
            fullscreenButton.visibility = View.GONE
        }
        //设置作者头像
        LoadImageUtil.loadUrl(
            holder.dataBinding.root.context,
            entity.avatar_url,
            holder.dataBinding.ivHead,
            ImgOptions.Builder.isCircle(true).build()
        )
        //评论次数
        holder.dataBinding.tvPingLunCount.text = if (entity.commentnum > 0) entity.commentnum.toString() else "1.3W+"

        var playImageView = holder.dataBinding.ivMusicPlayer
        //播放碟片图片
        LoadImageUtil.loadUrl(
            holder.dataBinding.root.context,
            entity.avatar_url,
            playImageView,
            ImgOptions.Builder.isCircle(true).build()
        )
        //旋转属性动画
        ObjectAnimator.ofFloat(playImageView, "Rotation", 0F, 360F).apply {
            duration = 1500 //动画执行时间
            repeatCount = ValueAnimator.INFINITE //无限循环
            interpolator = LinearInterpolator() //匀速效果
        }.start()

        //设置子组件点击事件
        holder.dataBinding.apply {
            ivHead.setOnClickListener{
                itemClickListener?.onItemChildClick(it, position)
            }

            ivAdd.setOnClickListener{
                itemClickListener?.onItemChildClick(it, position)
            }

            ivDianZanBtn.setOnClickListener{
                itemClickListener?.onItemChildClick(it, position)
            }

            ivPingLunBtn.setOnClickListener{
                itemClickListener?.onItemChildClick(it, position)
            }

            ivShouCangBtn.setOnClickListener{
                itemClickListener?.onItemChildClick(it, position)
            }

            ivShareBtn.setOnClickListener{
                itemClickListener?.onItemChildClick(it, position)
            }

            ivClose.setOnClickListener{
                itemClickListener?.onItemChildClick(it, position)
            }

            ivSend.setOnClickListener{
                itemClickListener?.onItemChildClick(it, position)
            }
        }

    }


    interface OnItemChildClickListener{
        fun onItemChildClick(v: View, position: Int)

    }


}